﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class SpinnerControl : UserControl, IControlRulable
    {
        private Spinner innerSpinner;

        internal Spinner InnerSpinner
        {
            get { return innerSpinner; }
            set { innerSpinner = value; }
        }

        public SpinnerControl()
        {
            InitializeComponent();
            this.innerSpinner = new Spinner();
            this.FillControls();
        }

        public SpinnerControl(Spinner spinner)
            : this()
        {
            this.innerSpinner = spinner;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.icAccelerationInterval.Value = this.innerSpinner.AccelerationInterval;
                this.rcAltDownArrow.Value = this.innerSpinner.AltDownArrow;
                this.rcAltUpArrow.Value = this.innerSpinner.AltUpArrow;
                this.rcCommand.Value = this.innerSpinner.Command;
                this.strDefaultUnit.Value = this.innerSpinner.DefaultUnit;
                this.dcDefaultValue.Value = this.innerSpinner.DefaultValue;
                this.strId.Value = this.innerSpinner.Id;
                this.strImeEnabled.Value = this.innerSpinner.ImeEnabled;
                this.icMultiplierInterval.Value = this.innerSpinner.MultiplierInterval;
                this.strQueryCommand.Value = this.innerSpinner.QueryCommand;
                this.integerControlSequence.Value = this.innerSpinner.Sequence;
                this.stringControlTemplateAlias.Value = this.innerSpinner.TemplateAlias;
                this.referencedControlToolTipDescription.Value = this.innerSpinner.ToolTipDescription;
                this.stringControlToolTipHelpKeyWord.Value = this.innerSpinner.ToolTipHelpKeyWord;
                this.referencedControlToolTipImage32by32.Value = this.innerSpinner.ToolTipImage32by32;
                this.referencedControlToolTipImage32by32Class.Value = this.innerSpinner.ToolTipImage32by32Class;
                this.integerControlToolTipImage32by32Left.Value = this.innerSpinner.ToolTipImage32by32Left;
                this.integerControlToolTipImage32by32Top.Value = this.innerSpinner.ToolTipImage32by32Top;
                this.stringControlToolTipShortcutKey.Value = this.innerSpinner.ToolTipShortcutKey;
                this.referencedControlToolTipTitle.Value = this.innerSpinner.ToolTipTitle;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerSpinner.AccelerationInterval = this.icAccelerationInterval.Value;
                    this.innerSpinner.AltDownArrow = this.rcAltDownArrow.Value;
                    this.innerSpinner.AltUpArrow = this.rcAltUpArrow.Value;
                    this.innerSpinner.Command = this.rcCommand.Value;
                    this.innerSpinner.DefaultUnit = this.strDefaultUnit.Value;
                    this.innerSpinner.DefaultValue = this.dcDefaultValue.Value;
                    this.innerSpinner.Id = this.strId.Value;
                    this.innerSpinner.ImeEnabled = this.strImeEnabled.Value;
                    this.innerSpinner.MultiplierInterval = this.icMultiplierInterval.Value;
                    this.innerSpinner.QueryCommand = this.strQueryCommand.Value;
                    this.innerSpinner.Sequence = this.integerControlSequence.Value;
                    this.innerSpinner.TemplateAlias = this.stringControlTemplateAlias.Value;
                    this.innerSpinner.ToolTipDescription = this.referencedControlToolTipDescription.Value;
                    this.innerSpinner.ToolTipHelpKeyWord = this.stringControlToolTipHelpKeyWord.Value;
                    this.innerSpinner.ToolTipImage32by32 = this.referencedControlToolTipImage32by32.Value;
                    this.innerSpinner.ToolTipImage32by32Class = this.referencedControlToolTipImage32by32Class.Value;
                    this.innerSpinner.ToolTipImage32by32Left = this.integerControlToolTipImage32by32Left.Value;
                    this.innerSpinner.ToolTipImage32by32Top = this.integerControlToolTipImage32by32Top.Value;
                    this.innerSpinner.ToolTipShortcutKey = this.stringControlToolTipShortcutKey.Value;
                    this.innerSpinner.ToolTipTitle = this.referencedControlToolTipTitle.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void SpinnerControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerSpinner.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
